﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Web.Mvc;
using QuanLyCongNo.ViewModel;

namespace QuanLyCongNo.Controllers
{
    public class QLCongNoController : BaseController
    {
        // GET: QLCongNo
        DataContext db = new DataContext();
       
        [HttpGet]
        public ActionResult Add()
        {
            DorSanpham menumode = new DorSanpham();
           
            ViewBag.ListSP = new SelectList(db.GetListSanPham().ToList(), "MaSP", "TenSP");
            ViewBag.ListSX = new SelectList(db.GetListSoXe().ToList(), "MaSX", "TenSX");
            return View();
        }
        [HttpPost]
        public ActionResult Add(CongNo model,Guid ListSP,Guid ListSX)
        {
            try
            {
                model.MaSP = ListSP;
                model.Ngay = DateTime.Now;
                model.MaSX = ListSX;
                model.MaCN = Guid.NewGuid();
                db.InsertCongNo(model);
                SetAlert("Thêm Thành Công", "success");
                ModelState.AddModelError("", "Cập nhật thành công !");
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                ViewBag.ListSP = new SelectList(db.GetListSanPham().ToList(), "MaSP", "TenSP");
                ViewBag.ListSX = new SelectList(db.GetListSoXe().ToList(), "MaSX", "TenSX");
                return View("Index", model);

            }
           
        }
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            ViewBag.ListSP = new SelectList(db.GetListSanPham().ToList(), "MaSP", "TenSP");
            ViewBag.ListSX = new SelectList(db.GetListSoXe().ToList(), "MaSX", "TenSX");
            var a = db.GetCongNoById(id);
           
            return View(a);
        }
        [HttpPost]
        public ActionResult Edit(CongNo model,Guid ListSP, Guid ListSX)
        {
            try
            {
                model.MaSP = ListSP;
                model.Ngay = DateTime.Now;
                model.MaSX = ListSX;
                model.MaCN = Guid.NewGuid();
                db.UpdateCongNo(model);
                SetAlert("Sửa Thành Công", "success");
                ModelState.AddModelError("", "Cập nhật Thành Công");

            }
            catch (Exception ex)
            {
              
                ModelState.AddModelError("", ex.Message);
               
            }

            return RedirectToAction("Index", model);
        }

        public ActionResult Delete(Guid id)
        {

            db.DeleteCongNo(id);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Index()
        {
            var a = db.GetListCongNo();
            return View(a);
        }

        public ActionResult Index(FormCollection formCollection)
        {
           

            return RedirectToAction("Index");
        }
    }
}